package com.example.core.utils;

import com.example.core.po.BasePo;

import java.io.Serializable;
import java.util.*;

public class CollectionUtils {

    public static List<String> convertKeyToList(Map<String, String> map) {
        Collection<String> values = map.keySet();
        return new ArrayList<>(values);
    }

    public static List<String> convertValueToList(Map<String, String> map) {
        Collection<String> values = map.values();
        return new ArrayList<>(values);
    }

    public static String[] convertToArray(List<String> list) {
        String[] arr = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }

    public static List convertToList(Serializable[] array) {
        List list = new ArrayList();
        for (Serializable ser : array) {
            list.add(ser);
        }
        return list;
    }

    public static String[] mapKeysToArray(Map<String, String> map) {
        return convertToArray(convertKeyToList(map));
    }

    public static String[] mapValuesToArray(Map<String, String> map) {
        return convertToArray(convertValueToList(map));
    }

    public static String[] exceptArray(String[] array, String key) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            String str = array[i];
            if (!str.equalsIgnoreCase(key)) {
                list.add(str);
            }
        }
        return convertToArray(list);
    }


    /****
     * 随机打乱List，再随即返回count个元素回来。
     * @param list
     * @param count
     * @return
     */
    public static List getShuffleList(List list, int count) {
        Collections.shuffle(list);
        return list.subList(0, count);
    }


    /****
     *
     * 返回count 个数据的list信息
     * @param count
     * @return
     */
    public static List randomList(int count) {
        List list = new ArrayList();
        Random random = new Random();
        do {
            int nextInt = random.nextInt(1000);
            list.add(nextInt + "");
        } while (list.size() < count);
        return list;
    }


    /***
     *
     * 随机返回下标
     * @param count
     * @return
     */
    public static int randomIndex(int count) {
        Random random = new Random();
        return random.nextInt(count);
    }

    /***
     * 获取两个list相同的部分.
     * @param list1
     * @param list2
     * @return
     */
    public static List<String> common(List list1, List list2) {
        List<String> list = new ArrayList<>();
        for (Object p1 : list1) {
            for (Object p2 : list2) {
                if (((BasePo)p1).getId().equalsIgnoreCase(((BasePo)p2).getId())) {
                    list.add(((BasePo)p1).getId());
                }
            }
        }
        return list;
    }
}
